package cn.tedu.v1.service.impl;

import cn.tedu.v1.mapper.DepartmentOfficeMapper;
import cn.tedu.v1.mapper.DoctorsMapper;
import cn.tedu.v1.model.DepartmentOffice;
import cn.tedu.v1.model.Patient;
import cn.tedu.v1.mapper.PatientMapper;
import cn.tedu.v1.service.IPatientService;
import cn.tedu.v1.vo.PatientVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-06
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {
   //添加一个病人
    @Resource
    private PatientMapper patientMapper;
    @Resource
    private DoctorsMapper doctorsMapper;
    QueryWrapper query = new QueryWrapper();
    @Resource
    private DepartmentOfficeMapper departmentOfficeMapper;
    @Value("${imagePath}")
    private String imagePath;
    @Override
    @Transactional
    public void addPatient(PatientVo patientVo) {
        //得到头像文件
        MultipartFile file = patientVo.getHeadImg();
        //获取文件名
        String picName = file.getOriginalFilename();
        //得到文件后缀名
        String suffix = picName.substring(picName.lastIndexOf("."));
        //得到唯一的文件名
        picName = UUID.randomUUID() + suffix;
        //创建一个以部门命名的的路径
        query.eq("office_id", patientVo.getDepartmentId());
        DepartmentOffice d = departmentOfficeMapper.selectOne(query);

        String dpName = d.getOfficeName() + "/" + patientVo.getDoctorId();
        String path = imagePath + "/" + dpName;

        //生成图片的保存路径
        String picPath = path + "/" + picName;
        Patient patient = new Patient();
        patient.setName(patientVo.getName())
                .setIdCard(patientVo.getIdCard())
                .setPhone(patientVo.getPhone())
                .setDepartmentId(patientVo.getDepartmentId())
                .setDoctorId(patientVo.getDoctorId())
                .setHeadImg("/" + dpName + "/" + picName)
                .setHistory(patientVo.getHistory())
                .setGender(patientVo.getGender());

         int num=patientMapper.insert(patient);//数据库操作成功后,再创建表示文件夹的文件对象
        File picfile = new File(picPath);
        //若文件夹不存在则创建
        if(num==1){
            if (!picfile.exists()) {
                picfile.mkdirs();
            }
            try {
                //把文件保存到上面的文件夹中
                file.transferTo(new File(picPath));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //获取所有的病人信息
    @Override
    @Transactional
    public PageInfo<Patient> getAllPatient(Integer pageNum,Integer pageSize) {
        query.clear();
        //选择不是删除状态的病人
        query.eq("status",1);
        //按照id降序排列
        query.orderByDesc("id");
        PageHelper.startPage(pageNum,pageSize);
        //查询出所有的病人的list集合
      List<Patient> patients= patientMapper.selectList(query);
      String doctorName;
      //遍历集合,因为前台需要显示每个病人的主治医生的姓名
      for(Patient p:patients){
          //根据病人对象中的doctorId再查询对应医生的姓名看,存入病人对象
          doctorName=doctorsMapper.selectById(p.getDoctorId()).getName();
          p.setDoctorName(doctorName);
      }
      log.debug("pageNum:"+pageNum+"   pageSize:"+pageSize);
        PageInfo<Patient> pageInfo=new PageInfo<>(patients);
        log.debug("pageInfo的信息:"+pageInfo);
      return pageInfo;
    }

    //编辑更改病人信息
    @Override
    public void updatePatient(PatientVo patientVo) {
        //得到头像文件
        MultipartFile file = patientVo.getHeadImg();
        //获取文件名
        String picName = file.getOriginalFilename();
        //得到文件后缀名
        String suffix = picName.substring(picName.lastIndexOf("."));
        //得到唯一的文件名
        picName = UUID.randomUUID() + suffix;
        //创建一个以部门命名的的路径
        query.clear();
        query.eq("office_id", patientVo.getDepartmentId());
        DepartmentOffice d = departmentOfficeMapper.selectOne(query);

        String dpName = d.getOfficeName() + "/" + patientVo.getDoctorId();
        String path = imagePath + "/" + dpName;

        //生成图片的保存路径
        String picPath = path + "/" + picName;
        Patient patient = new Patient();
        patient.setId(patientVo.getId())
                .setName(patientVo.getName())
                .setIdCard(patientVo.getIdCard())
                .setPhone(patientVo.getPhone())
                .setDepartmentId(patientVo.getDepartmentId())
                .setDoctorId(patientVo.getDoctorId())
                .setHeadImg("/" + dpName + "/" + picName)
                .setHistory(patientVo.getHistory())
                .setGender(patientVo.getGender());

        int num=patientMapper.updateById(patient);//数据库操作成功后,再创建表示文件夹的文件对象
        File picfile = new File(picPath);
        //若文件夹不存在则创建
        if(num==1){
            if (!picfile.exists()) {
                picfile.mkdirs();
            }
            try {
                //把文件保存到上面的文件夹中
                file.transferTo(new File(picPath));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void deletePatient(Integer id) {
        Patient patient=new Patient();
        patient.setId(id)
                .setStatus(0);
        int num=patientMapper.updateById(patient);
    }
}
